﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace BoobyTrap
{

    class BoobyConfig
    {
        public string img_path;
        public Image bg_img;
        public BoobyConfig()
        { 
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\BoobyTrap_Screensaver");
            if (key != null)
            {
                img_path = (string)key.GetValue("img_path");
                try
                {
                    bg_img = new Bitmap(img_path);
                }
                catch (System.ArgumentException ex)
                {
                    bg_img = (System.Drawing.Image)new System.ComponentModel.ComponentResourceManager(typeof(frmScreenSaver)).GetObject("$this.BackgroundImage");
                }
            }
            else
            {
                bg_img = (System.Drawing.Image)new System.ComponentModel.ComponentResourceManager(typeof(frmScreenSaver)).GetObject("$this.BackgroundImage");
            }
        }
    }
    static class Program
    {
        private struct RECT
        {
            public int left, top, right, bottom;
        }
        static public BoobyConfig conf;

        
        [DllImport("user32.dll")]
        private static extern bool GetClientRect(IntPtr hWnd, ref RECT rect);
        [DllImport("user32.DLL", EntryPoint = "IsWindowVisible")]
        private static extern bool IsWindowVisible(IntPtr hWnd);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0)
                return;
            conf = new BoobyConfig();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args[0].ToLower().Trim().Substring(0, 2) == "/c") // load the config stuff
            {
                Application.Run(new frmConfig());
            }
            else if (args[0].ToLower() == "/s") // load the screensaver
            {
                Application.Run(new frmScreenSaver());
            }
            else if (args[0].ToLower() == "/p") // load the preview
            {
                IntPtr parentHwnd;
                string owner_hwnd = args[1];
                parentHwnd = (IntPtr)uint.Parse(owner_hwnd);

                RECT rect = new RECT();
                if (GetClientRect(parentHwnd, ref rect))
                {
                    Graphics g = Graphics.FromHwnd(parentHwnd);
                    g.Clear(Color.Black);
                    g.DrawImage(Program.conf.bg_img, new Rectangle(0, 0, rect.bottom, rect.right));
                    g.Dispose();
                }

                while (IsWindowVisible(parentHwnd))
                    System.Threading.Thread.Sleep(100);
                Application.Exit();

                return;
            }
        }
    }
}
